package com.fingard.dsp.bank.directbank.baidu11.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Make {

    protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /**
     * 计算文件的MD5
     *
     * @param fileName 文件的绝对路径
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public static String getFileMD5String(String fileName, String Key) throws IOException, NoSuchAlgorithmException {
        File f = new File(fileName);
        return getFileMD5String(f, Key);
    }

    /**
     * 计算文件的MD5，重载方法
     *
     * @param file 文件对象
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public static String getFileMD5String(File file, String Key) throws IOException, NoSuchAlgorithmException {
        FileInputStream in = null;
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        try {
            in = new FileInputStream(file);
            byte[] buffer = new byte[1024 * 1024];
            byte[] keybuffer = new byte[1024 * 1024];
            keybuffer = Key.getBytes();
            messageDigest.update(keybuffer);
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                messageDigest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return bufferToHex(messageDigest.digest());
    }

    /**
     * 计算文件的MD5
     *
     * @param fileName 文件的绝对路径
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public static String getFileMD5String(String fileName) throws IOException, NoSuchAlgorithmException {
        File f = new File(fileName);
        return getFileMD5String(f);
    }

    /**
     * 计算文件的MD5，重载方法
     *
     * @param file 文件对象
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public static String getFileMD5String(File file) throws IOException, NoSuchAlgorithmException {
        FileInputStream in = null;
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        try {
            in = new FileInputStream(file);
            byte[] buffer = new byte[1024 * 1024];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                messageDigest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return bufferToHex(messageDigest.digest());
    }

    private static String bufferToHex(byte bytes[]) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte bytes[], int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;
        for (int l = m; l < k; l++) {
            appendHexPair(bytes[l], stringbuffer);
        }
        return stringbuffer.toString();
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 0xf0) >> 4];
        char c1 = hexDigits[bt & 0xf];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MD5Make md5 = new MD5Make();
        try {
            System.out.println(md5.getFileMD5String("d:/test/test1.txt"));
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
